from django.db import models
from django.utils import timezone


# Create your models here.

class User(models.Model):
    username = models.CharField('用户名', max_length=50)
    password = models.CharField('密码', max_length=50)
    email = models.CharField('邮箱', max_length=50, null=True, blank=True)
    role = models.CharField('角色', max_length=50)
    sex_choices = (
        (1, '男'),
        (2, '女'),
    )
    sex = models.IntegerField('性别', choices=sex_choices, default=1)
    entry_time = models.DateTimeField('入职时间', default=timezone.now, blank=True, null=True)
    phone = models.CharField(max_length=50, verbose_name='手机号', blank=True, null=True)

    class Meta:
        db_table = 'sys_user'

    def __str__(self):
        return self.username

class Depository(models.Model):
    dname = models.CharField(max_length=50)
    address = models.CharField(max_length=50)
    introduce = models.CharField(max_length=255)


    class Meta:
        db_table = 'sys_depository'

    def __str__(self):
        return self.dname

class Material(models.Model):
    mname = models.CharField(max_length=50)
    depository_id = models.ForeignKey(to = 'Depository', to_field='id', on_delete=models.CASCADE, verbose_name='仓库', null=True, blank=True)
    quantity = models.IntegerField(verbose_name='数量', null=True, blank=True)
    type_id = models.ForeignKey(to = 'Material_Type', to_field='id', on_delete=models.CASCADE, verbose_name='类型', null=True, blank=True)


    class Meta:
        db_table = 'sys_material'

    def __str__(self):
        return self.mname

class Material_Type(models.Model):
    tname = models.CharField(max_length=50, verbose_name='类型名称')
    introduce = models.CharField(max_length=255, verbose_name='类型介绍')
    class Meta:
        db_table = 'sys_material_type'

    def __str__(self):
        return self.tname


class Depository_record(models.Model):
    mname = models.CharField('物品名称', max_length=50)
    depository_id = models.ForeignKey(to="Depository", to_field="id", on_delete=models.SET_NULL, verbose_name="仓库id", null=True)
    dispatch_choices = (
        (1, '出库'),
        (0, '入库')
    )
    dispatch = models.IntegerField(choices=dispatch_choices, default=1, verbose_name='出入库')
    quantity = models.IntegerField('数量', null=True, blank=True)
    state = models.CharField('状态', max_length=50)
    applicant_id = models.ForeignKey(to="User", to_field="id", on_delete=models.SET_NULL, verbose_name="申请人id", null=True)
    apply_remark = models.CharField('申请备注', max_length=255)
    apply_time = models.DateTimeField('申请时间', default=timezone.now)
    class Meta:
        db_table = 'sys_depository_record'

    def __str__(self):
        return self.mname

class Examine_record(models.Model):
    record_id = models.ForeignKey(to="Depository_record", to_field="id", on_delete=models.CASCADE, verbose_name="记录id")
    reviewer_id = models.ForeignKey(to="User", to_field="id", on_delete=models.SET_NULL, verbose_name="审核人id", null=True)
    reviewer_remark = models.CharField('审核备注', max_length=255, null=True, blank=True)
    reversed_pass_choices = (
        (1, '通过'),
        (0, '不通过')
    )
    reversed_pass = models.IntegerField(choices=reversed_pass_choices, default=0, verbose_name='是否通过')
    review_time = models.DateTimeField('审核时间', default=timezone.now)
    class Meta:
        db_table = 'sys_examine_record'

    def __str__(self):
        return self.record_id